package com.springboot.admin.controller;

import com.springboot.admin.common.ResponseMap;
import com.springboot.admin.service.PagesSerice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/pages")
@Api(tags = "文章相关接口")
public class PagesController {

  @Autowired
  PagesSerice pagesSerice;

  @GetMapping("/getList/{page}")
  @ApiOperation(value="查询首页分页")
  public ResponseMap getListPage(@PathVariable("page") String page)  {
    return ResponseMap.success(pagesSerice.getListByPage(page));
  }

  @GetMapping("/getDetail/{id}")
  @Cacheable(value = "getDetail")
  @ApiOperation(value="查询详情")
  public ResponseMap getDetail(@PathVariable("id") String id) {
    return ResponseMap.success(pagesSerice.getDetailByPageId(id));
  }

  @GetMapping("/getList/cate/{cate}/page/{page}")
  @Cacheable(value = "getListCatePage")
  @ApiOperation(value="查询分类分页列表")
  public ResponseMap getList(@PathVariable("cate") String cate, @PathVariable("page") String page) {
    return ResponseMap.success(pagesSerice.getListByCatePage(cate,page));
  }

  @ApiOperation(value="查询标签分页列表")
  @GetMapping("/getList/tags/{cate}/page/{page}")
  @Cacheable(value = "getListTagsPage")
  public ResponseMap getListByTags(@PathVariable("cate") String cate, @PathVariable("page") String page) throws UnsupportedEncodingException {
    return ResponseMap.success(pagesSerice.getListByTagsPage(cate,page));
  }

  @ApiOperation(value="查询推荐阅读")
  @PostMapping("/getRecomList/{type}")
  @Cacheable(value = "getRecomList")
  public ResponseMap getRecomList(@PathVariable("type") String type) {
    return ResponseMap.success(pagesSerice.getRecomList(type));
  }

  @ApiOperation(value="查询所有分类")
  @GetMapping("/getCate")
  @Cacheable(value = "getCate")
  public ResponseMap getCate() {
    return ResponseMap.success(pagesSerice.getCateList());
  }

  @ApiOperation(value="查询标签云")
  @GetMapping("/getTags")
  @Cacheable(value = "getTags")
  public ResponseMap getTags() {
    return ResponseMap.success(pagesSerice.getTagsList());
  }

  @ApiOperation(value="获取文章评论")
  @GetMapping("/getComment")
  @Cacheable(value = "getComment")
  public ResponseMap getComment(@RequestParam String id) {
    return ResponseMap.success(pagesSerice.getCommentById(id));
  }

  @ApiOperation(value="获取所有文章评论")
  @GetMapping("/getAllComment")
  public ResponseMap getAllComment() {
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("result", pagesSerice.getAllComment());
    return ResponseMap.success(resultMap);
  }

  @ApiOperation(value="获取最新评论")
  @GetMapping("/getLatestComments")
  public ResponseMap getLatestComments() {
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("result", pagesSerice.getLatestComments());
    return ResponseMap.success(resultMap);
  }

  @ApiOperation(value="下载文件")
  @PostMapping("/download")
  public String download(HttpServletRequest request, HttpServletResponse response) {
    // 获取文件名
    try {
      String fileName = "555.png";
      String filePath = "./uploadFile/";
      String path = filePath + fileName;
      File file = new File(path);
      if (file.exists()) {
        response.setContentType("application/force-download");// 设置强制下载不打开
        response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
        byte[] buffer = new byte[1024];
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
          fis = new FileInputStream(file);
          bis = new BufferedInputStream(fis);
          OutputStream os = response.getOutputStream();
          int i = bis.read(buffer);
          while (i != -1) {
            os.write(buffer, 0, i);
            i = bis.read(buffer);
          }
          return "下载成功";
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return "下载失败";
  }
}
